package com.uniriho.project.lxr0860gzzmb_ic_test;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.uniriho.project.toolkit.BluetoothMessage;

import java.util.ArrayList;

/**
 * @Description: TODO<自定义适配器Adapter,作为listview的适配器>
 * @author
 * @data:
 * @version:
 */
public class LeDeviceListAdapter extends BaseAdapter {

    // 蓝牙信号强度
    private ArrayList<Integer> rssis;

    private ArrayList<BluetoothMessage> mLeDevices;

    private LayoutInflater mInflator;
    private Context mContext;

    public LeDeviceListAdapter(Context context)
    {
        super();
        this.mContext = context;
        rssis = new ArrayList<Integer>();
        mLeDevices = new ArrayList<BluetoothMessage>();
//            mInflator = getLayoutInflater();
        mInflator = LayoutInflater.from(context);
    }

    public void addDevice(BluetoothMessage device, int rssi)
    {
        if (mLeDevices.contains(device)) {
            return;
        }

        mLeDevices.add(device);
        rssis.add(rssi);
    }

    public BluetoothMessage getDevice(int position)
    {
        return mLeDevices.get(position);
    }

    public void clear()
    {
        mLeDevices.clear();
        rssis.clear();
    }

    @Override
    public int getCount()
    {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i)
    {
        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i)
    {
        return i;
    }

    /**
     * 重写getview
     *
     * **/
    @SuppressLint("MissingPermission")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {

        // General ListView optimization code.
        // 加载listview每一项的视图
        view = mInflator.inflate(R.layout.listitem, null);
        // 初始化三个textview显示蓝牙信息
        TextView deviceAddress = (TextView) view
                .findViewById(R.id.tv_deviceAddr);
        TextView deviceName = (TextView) view
                .findViewById(R.id.tv_deviceName);
        TextView rssi = (TextView) view.findViewById(R.id.tv_rssi);

        BluetoothMessage bluetoothMessage = mLeDevices.get(i);
        deviceAddress.setText(bluetoothMessage.getDevice().getAddress());
        deviceName.setText(bluetoothMessage.getName()!=null?bluetoothMessage.getName():bluetoothMessage.getDevice().getName());
        rssi.setText("" + rssis.get(i));

        return view;
    }

    public Integer getRssis(int position){
        return rssis.get(position);
    }
}